var count = 50;
$("#step_1 .btn_ok").off("click").on("click",function(){
    $(".pop_reg").css("display","block");
    $("#step_1").css("display","none");
    $(".reg_btn").click();
})

$(".login_btn").off("click").on("click",function(){
    console.log($(".mask_b"));
    var customer = {};
    $(".pop_login").html("");
    var phoneReg = /^1[34578]\d{9}$/;
    var validateCodeReg = /^\d{6}$/;
    var pwdReg = /^[\w!@#$%^&*()_-]{6,20}$/;
    var loginInfo = '<p><a href="#" class="btn btn_back chfont">返回</a></p>'+
        '<div class="reg_box mart50" style="display: ;">'+
        '<table width="100%">'+
        '<tr>'+
        '<th>手机号</th>'+
        '<td>'+
        '<div class="has-general clearfix">'+
        '<input id="phoneNumberLogin" type="text" placeholder="">'+
        '</div>'+
        '<label class="control-label">请输入有效的11位手机号码</label>'+
        '</td>'+
        '</tr>'+
        '<tr>'+
        '<th>密码</th>'+
        '<td>'+
        '<div class="has-general clearfix">'+
        '<input id="pwdLogin" type="password" placeholder="">'+
        '</div>'+
        '<label class="control-label" title="出错加has-error">密码不正确</label>'+
        '</td>'+
        '</tr>'+
        '<tr>'+
        '<th></th>'+
        '<td>'+
    '<a href="#" id="forgetPwd" class="lose font12 floatright">忘记密码</a>'+
        '</td>'+
        '</tr>'+
        '</table>'+
        '<p class="aligncenter mart10 login_reg">'+
        '<a href="#" class="btn btn_login marr10">登 录</a>'+
    '<a href="#" class="btn btn_reg">注 册</a>'+
    '</p>'+
    '</div>';
    $(".mask_b").css("display","block");
    $(".pop_login").css("display","block");
    $(".pop_login").append(loginInfo);
    $(".pop_login #phoneNumberLogin").blur(function(){
        var value = $(this).val();
        customer.loginName = value;
        if(value.trim()!=""){
            if(!phoneReg.test(value)){
                $("#phoneNumberLogin").parent().next().addClass("has-error").html("请输入有效的11位手机号码");
                phoneFlag = true;
            }else{
                $("#phoneNumberLogin").parent().next().removeClass("has-error");
            }
        }
    });

    $(".pop_login .btn_login").off("click").on("click",function(){
        var customer = {};
        var flag = false;
        customer.loginName = $("#phoneNumberLogin").val();
        customer.password = $("#pwdLogin").val();

        if(!phoneReg.test(customer.loginName)){
            $("#phoneNumberLogin").parent().next().addClass("has-error");
            flag = true;
        }else{
            $("#phoneNumberLogin").parent().next().removeClass("has-error");
        }

        if(customer.password===""){
            $("#pwdLogin").parent().next().addClass("has-error").html("请输入密码");
            flag = true;
        }else{
            $("#pwdLogin").parent().next().removeClass("has-error");
        }

        if(flag){
            return false;
        }

        var promise = httpService.get({
            url:url.login,
            data:customer
        });
        promise.then(function(response){
            if(response.success){
                common.setCookie("loginName",response.loginName);
                $(".pop_login").css("display","none");
                $(".mask_b").css("display","none");
                $(".login_info").css("display","none");
                $(".login_ok .user_name").html("Hi,"+response.loginName);
				$(".login_ok").css("display","block");
                $(".pop_login").html("");

            }else{
                $("#pwdLogin").parent().next().addClass("has-error").html(response.msg);

            }
        },function(){
            $("#pwdLogin").parent().next().addClass("has-error");
        })

    });
    $(".pop_login .btn_back").off("click").on("click",function(){
        $(".mask_b").css("display","none");
        $(".pop_login").css("display","none");
        $(".pop_login").html("");
    });

    $(".btn_reg").off("click").on("click",function(){
        //$(".reg_btn").click();
        $(".pop_login").css("display","none");
        $(".reg_btn").click();
    });
    //$(".pop_reg .btn_back").off("click").on("click",function(){
    //    $(".mask_b").css("display","none");
    //    $("#pop_reg").css("display","none");
    //})
});



$(".reg_btn").off("click").on("click",function(){
    var registerFlag = $(this).attr("data-register");
    var customer = {};
    var phoneReg = /^1[34578]\d{9}$/;
    var validateCodeReg = /^\d{6}$/;
    var pwdReg = /^[\w!@#$%^&*()_-]{6,20}$/;
    var reginInfo = '<p>'+
    '<a href="#" class="btn btn_back chfont">返回</a>'+
       '</p>'+
        '<div class="reg_box reg_phoneNumber" style="display:;">'+
        '<table width="100%">'+
        '<tr>'+
        '<th>手机号</th>'+
        '<td>'+
        '<div class="has-general clearfix">'+
        '<input id="phoneNumber" type="text" placeholder="">'+

        '</div> <label class="control-label" style="display: block">请输入有效的11位手机号码</label>'+
        '</td>'+
        '</tr>'+
        '<tr>'+
        '<th>验证码</th>'+
        '<td>'+
        '<div class="has-general clearfix">'+
        '<input id="validateCode" type="text" placeholder=""> <a href="javascript:;" class="code" style="display:;">获取验证码</a> <span class="code code_wait" style="display: none"><em>50</em>秒</span>'+
        '</div> <label class="control-label" title="出错加has-error">请输入有效的验证码</label>'+
        '</td>'+
        '</tr>'+
        '</table>'+
        '<p class="alignright mart10">'+
       '<a href="#" class="btn btn_next"></a>'+
        '</p>'+
        '</div>'+
        '<div class="reg_box reg_pwd" style="display: none;">'+
        '<table class="mart60" width="100%">'+
        '<tr>'+
        '<th>设置密码</th>'+
        '<td>'+
        '<div class="has-general  clearfix">'+
        '<input id="pwd" type="password" placeholder="">'+
        '</div> <label class="control-label" title="出错加has-error" style="display: block">密码需6到20位字符，由英文和中文结合填写</label>'+
    '</td>'+
    '</tr>'+
    '<tr>'+
    '<th>确认密码</th>'+
    '<td>'+
    '<div class="has-general  clearfix">'+
        '<input id="rePwd" type="password" placeholder="">'+
        '</div> <label class="control-label">密码不匹配</label>'+
        '</td>'+
        '</tr>'+
        '</table>'+
        '<p class="alignright mart20">'+
        '<a href="#" class="btn btn_commit"></a>'+
        '</p>'+
        '</div>';
    $(".mask_b").css("display","block");
    $(".pop_reg").css("display","block");
    $(".pop_reg").append(reginInfo);

    //注册开始
    //$(".reg_btn").on("blur","#phoneNumber",function(){

$("#phoneNumber").blur(function(){
        var value = $(this).val().trim();

        if(value!=""){
            customer.loginName = value;
            customer.mobile = value;
            if(!phoneReg.test(value)){
                $("#phoneNumber").parent().next().addClass("has-error").html("请输入有效的11位手机号码");
                phoneFlag = true;
            }else{
                var promise = httpService.get({
                    url:url.validMobile,
                    data:customer
                });
                promise.then(function(response){
                    if(response.success){
                        $("#phoneNumber").parent().next().addClass("txt-succ").html(response.msg);
                        $("#phoneNumber").parent().next().removeClass("has-error").html();
                    }else{
                        $("#phoneNumber").parent().next().removeClass("txt-succ").addClass("has-error").html(response.msg);
                    }

                },function(){
                    $("#phoneNumber").parent().next().addClass("has-error").html("请求失败");
                })
            }
        }
    })

    $(".pop_reg .code").off("click").on("click",function(){
        if($("#phoneNumber").parent().next().hasClass("txt-succ")){
            var promise = httpService.get({
                url:url.getvalidcode,
                data:customer
            });
            promise.then(function(response){
                $(".code").css("display","none");
                $(".code_wait").css("display","block");
                countId = setInterval("countFun()",1000);
            },function(){
                $("#phoneNumber").parent().next().addClass("has-error").html("请求失败");
            })
        }
    });

    $(".btn_next").off("click").on("click",function(){
        customer.loginName = $("#phoneNumber").val();
        customer.validationCode = $("#validateCode").val();

        var flag = false;
        if(!phoneReg.test(customer.loginName)){
            $("#phoneNumber").parent().next().addClass("has-error").html("请输入有效的11位手机号码");;
            flag = true;
        }else{
            $("#phoneNumber").parent().next().removeClass("has-error");
        }
        if(!validateCodeReg.test(customer.validationCode)){
            $("#validateCode").parent().next().addClass("has-error");
            flag = true;
        }else{
            $("#validateCode").parent().next().removeClass("has-error");
        }
        if(flag){
            return false;
        }else{
            customer.isValidCode = 0;
            var promise = httpService.get({
                url:url.regist,
                data:customer
            });
            promise.then(function(response){
                $(".reg_phoneNumber").css("display","none");
                $(".reg_pwd").css("display","block");
            },function(){

            })
        }
    });

    $(".btn_commit").off("click").on("click",function(){
        customer.password = $("#pwd").val();
        customer.rePwd = $("#rePwd").val();

        var flag = false;
        if(!pwdReg.test(customer.password)){
            $("#pwd").parent().next().addClass("has-error");
            flag = true;
        }else{
            $("#pwd").parent().next().removeClass("has-error");
        }

        if(customer.password !="" && customer.password!==customer.rePwd){
            $("#rePwd").parent().next().addClass("has-error");
            flag = true;
        }else{
            $("#rePwd").parent().next().removeClass("has-error");
        }
        if(flag){
            return false;
        }
        customer.isValidCode = 1;
        var promise = httpService.post({
            url:url.regist,
            data:customer
        });
        promise.then(function(response){
        	if (response.success) {
        		common.setCookie("loginName",response.loginName);
        		$(".pop_reg").css("display","none");
        		$(".login_info").css("display","none");
        		$(".login_ok .user_name").html("Hi,"+response.loginName);
        		$(".login_ok").css("display","block");
        		$(".pop_reg").html("");
        		if(registerFlag==1){
        			if(window.location.href!="http://localhost:8080/duodao_website/"||window.location.href!="http://localhost:8080/duodao_website/pages/index.html"){
        				window.location = "/duodao_website/pages/buildMyHome.html";
        			}
        			$("#step_2").css("display","block");
        			$(".mask_b").css("display","block");
        		}else{
        			$(".mask_b").css("display","none");
        		}
			}else{
				$("#rePwd").parent().next().addClass("has-error").html(response.msg);
			}
        },function(){

        })
    });

    $(".pop_reg .btn_back").off("click").on("click",function(){
        $(".mask_b").css("display","none");
        $(".pop_reg").css("display","none");
        $(".pop_reg").html("");
    });

});

function personalCenterClick(){
    promise = httpService.get({
        url: url.sessionTimeOut
    });
    promise.then(function(response){
        if(response.success){
           window.location.href = "/duodao_website/pages/personalCenter.html";
        }else{
            common.deleteCookie("loginName");
            window.location.href = "/duodao_website/pages/index.html";
        }
    })
}


function myHomeClick(){
    promise = httpService.get({
        url: url.sessionTimeOut
    });
    promise.then(function(response){
        if(response.success){
            window.location.href = "/duodao_website/pages/myHome.html";
        }else{
            common.deleteCookie("loginName");
            window.location.href = "/duodao_website/pages/index.html";
        }
    })
}

$(".logout").off("click").on("click",function(){
    var promise = httpService.get({
        url: url.loginout
    });

    promise.then(function (response) {
        if(response.success){
            common.deleteCookie("loginName");
            window.location.href="/duodao_website/pages/index.html";
        }
    });
});

function countFun(){
    count--;
    $(".code_wait").html(count+"秒");
    if(count<=0){
        clearInterval(countId);
        count = 50;
        $(".code").css("display","block");
        $(".code_wait").css("display","none");
    }

}